<template>
    <div class="matchground">
        <div class="row">
            <div class="col-4">
                <div class="user-photo">
                    <img :src="$store.state.user.photo" alt="">
                </div>
                <div class="user-username">
                    {{ $store.state.user.username }}
                </div>
            </div>
            <div class="col-4">
                <div v-if="match_btn_info === '开始匹配'" class="user-select_bot">
                    <select v-model="select_bot" class="form-select" aria-label="Default select example">
                        <option value="-1" selected>我自己操作</option>
                        <option v-for="bot in bots" :key="bot.id" :value="bot.id">
                        {{ bot.title }}
                        </option>
                    </select>
                </div>
            </div>
            <div class="col-4">
                <div class="user-photo">
                    <img :src="$store.state.pk.opponent_photo" alt="">
                </div>
                <div class="user-username">
                    {{ $store.state.pk.opponent_username  }}
                </div>
            </div>
            <div class="col-12" style="text-align: center; margin-top: 15vh;" >
                <button @click="click_match_btn()" type="button" class="btn btn-secondary btn-lg">
                    {{ match_btn_info }}
                </button>
            </div>
        </div>
    </div>
</template>


<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import $ from 'jquery';

export default{
    components: {
    },

    setup(){
        const store = useStore();//拿出全局变量
        let match_btn_info = ref("开始匹配");

        let bots = ref([]);
        let select_bot = ref("-1");

        const click_match_btn = () =>{
            if(match_btn_info.value === "开始匹配"){
                match_btn_info.value = "取消";
                store.state.pk.socket.send(JSON.stringify({//用到了socket所以记得在一开始把socket加到全局变量里
                    event: "start-matching",
                    bot_id: select_bot.value,
                }))
            }else {
                store.state.pk.socket.send(JSON.stringify({
                    event: "stop-matching",

                }))
                match_btn_info.value = "开始匹配";
            }
        }

        
        const refresh_bots = () => {//刷新页面(不刷新网页)
                $.ajax({
                    url: "https://app5541.acapp.acwing.com.cn/api/user/bot/getList/",
                    type: "get",
                    headers:{
                        Authorization: "Bearer " + store.state.user.token,
                    },
                    success(resp){
                        bots.value = resp;
                    },
                    error(resp){
                        console.log("获取bot列表出错");
                        console.log(resp);
                    }
                })
        }

        refresh_bots();//从云端获取bot(post)

        return {
            match_btn_info,
            click_match_btn,
            bots,
            select_bot

        }


    }
}
</script>

<style>
div.matchground{
    width: 70vw;
    height: 60vh;
    background: rgba(137, 207, 240, 0.5);
    margin: 60px auto;
}

div.user-photo{
    text-align: center;
    padding-top: 10vh;
}
div.user-photo>img{
    border-radius: 50%;
    width: 20vh;
    
}
div.user-username{
    text-align: center;
    font-size: 24px;
    font-weight: 600;
    padding-top: 2vh;
}
div.user-select_bot{
    padding-top: 5vh;
}
div.user-select_bot > select{
    width: 60%;
    margin: 0 auto;
}
</style>